package com.application.lucky.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.activity.AccountSecurityActivity;
import com.application.lucky.activity.MyBaoBiaoActivity;
import com.application.lucky.activity.UserCenterActivity;
import com.application.lucky.activity.me.DaiLiActivity;
import com.application.lucky.activity.me.MoneyHistoryActivity;
import com.application.lucky.activity.me.PerfectDataActivity;
import com.application.lucky.activity.me.RechargeCardActivity;
import com.application.lucky.activity.me.SettingLuckyActivity;
import com.application.lucky.activity.me.TIXianActivity;
import com.application.lucky.bean.UserBean;
import com.application.lucky.bean.eventbean.UpdateUserInfo;
import com.application.lucky.config.Config;
import com.application.lucky.gen.UserBeanDao;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.request.UserRequest;
import com.application.lucky.request.nav.UrlManager;
import com.application.mylibrary.base.BaseFragment;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.LogUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.qunar.im.ui.util.ProfileUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class FragmentMe extends BaseFragment implements View.OnClickListener {
    private android.widget.ImageView mIvFragmentMeThumb;
    private android.widget.TextView mTvFragmentMeName;
    private android.widget.TextView tv_user_nick_name;
    private android.widget.RelativeLayout mRlFragmentMeMoney;
    private android.widget.RelativeLayout mRlFragmentMeZhongzhi;
    private android.widget.RelativeLayout mRlFragmentMeTixian;
    private android.widget.TextView mTvFragmentMePerson;
    private android.widget.TextView mTvFragmentMeDaili;
    private android.widget.TextView mTvFragmentMeZijin;
    private android.widget.TextView mTvFragmentMeBaobiao;
    private android.widget.TextView mTvFragmentMeAnquan;
    private android.widget.TextView mTvFragmentMeHelp;
    private android.widget.TextView mTvFragmentMeSetting;
    private android.widget.TextView tv_money;
    private TextView tvId;

    @Override
    protected void beforeInit(Bundle arguments) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fragment_me;
    }

    @Override
    public void initView(View view) {
        EventBusUtils.register(this);
        mIvFragmentMeThumb = view.findViewById(R.id.iv_fragment_me_thumb);
        mTvFragmentMeName = view.findViewById(R.id.tv_fragment_me_name);
        tv_user_nick_name = view.findViewById(R.id.tv_user_nick_name);
        mRlFragmentMeMoney = view.findViewById(R.id.rl_fragment_me_money);
        mRlFragmentMeZhongzhi = view.findViewById(R.id.rl_fragment_me_zhongzhi);
        mRlFragmentMeTixian = view.findViewById(R.id.rl_fragment_me_tixian);
        mTvFragmentMePerson = view.findViewById(R.id.tv_fragment_me_person);
        mTvFragmentMeDaili = view.findViewById(R.id.tv_fragment_me_daili);
        mTvFragmentMeZijin = view.findViewById(R.id.tv_fragment_me_zijin);
        mTvFragmentMeBaobiao = view.findViewById(R.id.tv_fragment_me_baobiao);
        mTvFragmentMeAnquan = view.findViewById(R.id.tv_fragment_me_anquan);
        mTvFragmentMeHelp = view.findViewById(R.id.tv_fragment_me_help);
        mTvFragmentMeSetting = view.findViewById(R.id.tv_fragment_me_setting);
        tv_money = view.findViewById(R.id.tv_money);
        String userName = UserManagers.getInstance().getShowName();
        String userId = UserManagers.getInstance().getUserId();

        mTvFragmentMeName.setText(userName);
        tv_user_nick_name.setText(UserManagers.getInstance().getUserName());
        initListener();
        ProfileUtils.displayGravatarRoundedCornerByImageSrc(getActivity(), UrlManager.getHeadPic(UserManagers.getInstance().getPic()), mIvFragmentMeThumb);
        tvId = ((TextView) view.findViewById(R.id.tv_user_id));
        setUid(userId);
    }

    private void setUid(String uid) {
        try {
            Long integer = Long.valueOf(uid);
            tvId.setText("id：" + (integer ^ Config.ID_proguard));
        } catch (Exception e) {
            tvId.setText("id：" + uid);
        }


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void userUp(UserBean bean) {
        ProfileUtils.displayGravatarRoundedCornerByImageSrc(getActivity(), UrlManager.getHeadPic(bean.getHeadPic()), mIvFragmentMeThumb);
        setUid(bean.getUserId() + "");

        mTvFragmentMeName.setText(bean.getNickName());
    }

    @Override
    public void onResume() {
        super.onResume();
        double balance = UserManagers.getInstance().getBalance();
        tv_money.setText("¥" + balance);
        new RedPRequest().red_balance(new CallBackEx<BaseResponse<Integer>>() {
            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onSuccess(BaseResponse<Integer> integerBaseResponse) {
                UserManagers.getInstance().setBalance(integerBaseResponse.dataInfo);
                double balance = UserManagers.getInstance().getBalance();
                tv_money.setText("¥" + balance);
            }
        });
    }

    private void initListener() {
        mTvFragmentMePerson.setOnClickListener(this);
        mTvFragmentMeDaili.setOnClickListener(this);
        mTvFragmentMeZijin.setOnClickListener(this);
        mTvFragmentMeBaobiao.setOnClickListener(this);
        mTvFragmentMeAnquan.setOnClickListener(this);
        mTvFragmentMeHelp.setOnClickListener(this);
        mTvFragmentMeSetting.setOnClickListener(this);
        mIvFragmentMeThumb.setOnClickListener(this);
        mRlFragmentMeZhongzhi.setOnClickListener(this);
        mRlFragmentMeTixian.setOnClickListener(this);
        mRlFragmentMeMoney.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_fragment_me_thumb:
                LogUtils.i("我的头像");
                startActivityAnim (new Intent(mCOntext, UserCenterActivity.class));

                break;
            case R.id.rl_fragment_me_money:
                LogUtils.i("余额");
                break;
            case R.id.rl_fragment_me_tixian:
                startActivityAnim (new Intent(mCOntext, TIXianActivity.class));
                LogUtils.i("提现");
                break;
            case R.id.rl_fragment_me_zhongzhi:
                startActivityAnim (new Intent(mCOntext, RechargeCardActivity.class));
                LogUtils.i("充值");
                break;
            case R.id.tv_fragment_me_person:
                startActivityAnim (new Intent(mCOntext, PerfectDataActivity.class));
                LogUtils.i("个人资料");
                break;
            case R.id.tv_fragment_me_zijin:
                startActivityAnim (new Intent(mCOntext, MoneyHistoryActivity.class));
                LogUtils.i("资金");
                break;
            case R.id.tv_fragment_me_help:
                LogUtils.i("帮助");
                break;
            case R.id.tv_fragment_me_setting:
                Intent settingIntent = new Intent(mCOntext, SettingLuckyActivity.class);
                startActivityAnim (settingIntent);
                LogUtils.i("设置");
                break;
            case R.id.tv_fragment_me_daili:
                startActivityAnim (new Intent(mCOntext, DaiLiActivity.class));
                LogUtils.i("代理");
                break;
            case R.id.tv_fragment_me_baobiao:
                startActivityAnim(new Intent(mCOntext, MyBaoBiaoActivity.class));
                break;
            case R.id.tv_fragment_me_anquan:
                LogUtils.i("安全");
//                UserManagers.getInstance().loginOut();
                startActivityAnim (new Intent(mCOntext, AccountSecurityActivity.class));
                break;
        }
    }

    @Override
    public void onDestroy() {
        EventBusUtils.unregister(this);
        super.onDestroy();
    }

    @Subscribe
    public void updateUserInfo(UpdateUserInfo event) {
        new UserRequest().getSelfData(new CallBackEx<BaseResponse<UserBean>>() {
            @Override
            public void onSuccess(BaseResponse<UserBean> userBeanBaseResponse) {
                if (userBeanBaseResponse.dataInfo != null) {
                    ProfileUtils.displayGravatarRoundedCornerByImageSrc(getActivity(), UrlManager.getHeadPic(userBeanBaseResponse.dataInfo.getHeadPic()), mIvFragmentMeThumb);
                    mTvFragmentMeName.setText(userBeanBaseResponse.dataInfo.getShowName());
                    UserBeanDao userBeanDao = Lucky.getApplication().getDaoSession().getUserBeanDao();
                    if (userBeanDao != null) {
                        UserBean userBean = UserManagers.getInstance().getUserBean();
                        userBean.setBirthday(userBeanBaseResponse.dataInfo.getBirthday());
                        userBean.setNickName(userBeanBaseResponse.dataInfo.getNickName());
                        userBean.setCity(userBeanBaseResponse.dataInfo.getCity());
                        userBean.setShowName(userBeanBaseResponse.dataInfo.getShowName());
                        userBeanDao.insertOrReplace(userBean);
                    }
                }
            }

            @Override
            public void onFailed(int code) {

            }
        });
    }
}
